package com.easyjava.builder;

import com.easyjava.bean.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BuildBase
 * @Date 2024/6/4  15:42
 **/
public class BuildBase {
    private static Logger logger = LoggerFactory.getLogger(BuildBase.class);

    public static void execute() {
        List<String> headInfoList = new ArrayList<>();

        //生成date枚举
        headInfoList.add("package " + Constants.PACKAGE_ENUMS);
        build(headInfoList, "DateTimePatternEnum", Constants.PATH_ENUMS);


        headInfoList.clear();
        headInfoList.add("package " + Constants.PACKAGE_UTILS);
        build(headInfoList, "DateUtil", Constants.PATH_UTILS);

        //生成baseMapper
        headInfoList.clear();
        headInfoList.add("package " + Constants.PACKAGE_MAPPER);
        build(headInfoList, "BaseMapper", Constants.PATH_MAPPER);


        //生成pageSize枚举
        headInfoList.clear();
        headInfoList.add("package " + Constants.PACKAGE_ENUMS);
        build(headInfoList, "PageSize", Constants.PATH_ENUMS);


        //生成pageSize枚举
        headInfoList.clear();
        headInfoList.add("package " + Constants.PACKAGE_QUERY);
        headInfoList.add("import " + Constants.PACKAGE_ENUMS + ".PageSize");
        build(headInfoList, "SimplePage", Constants.PATH_QUERY);



        //生成pageSize枚举
        headInfoList.clear();
        headInfoList.add("package " + Constants.PACKAGE_QUERY);
        build(headInfoList, "BaseQuery", Constants.PATH_QUERY);



        //生成PaginationResultVO
        headInfoList.clear();
        headInfoList.add("package " + Constants.PACKAGE_VO);
        build(headInfoList, "PaginationResultVO", Constants.PATH_VO);


        //生成exception枚举
        headInfoList.clear();
        headInfoList.add("package " + Constants.PACKAGE_VO);
        build(headInfoList, "ResponseVo", Constants.PATH_VO);

        //生成exception枚举
        headInfoList.clear();
        headInfoList.add("package " + Constants.PACKAGE_EXCEPTION);
        headInfoList.add("import " + Constants.PACKAGE_ENUMS + ".ResponseCodeEnum");
        build(headInfoList, "BusinessException", Constants.PATH_EXCEPTION);

        //生成ResponseCodeEnum枚举
        headInfoList.clear();
        headInfoList.add("package " + Constants.PACKAGE_ENUMS);
        build(headInfoList, "ResponseCodeEnum", Constants.PATH_ENUMS);

        //生成BaseController枚举
        headInfoList.clear();
        headInfoList.add("package " + Constants.PACKAGE_CONTROLLER);
        headInfoList.add("import " + Constants.PACKAGE_ENUMS + ".ResponseCodeEnum");
        headInfoList.add("import " + Constants.PACKAGE_VO + ".ResponseVO");
        build(headInfoList, "ABaseController", Constants.PATH_CONTROLLER);

        //生成AGlobalExceptionHandlerController
        headInfoList.clear();
        headInfoList.add("package " + Constants.PACKAGE_CONTROLLER);
        headInfoList.add("import " + Constants.PACKAGE_ENUMS + ".ResponseCodeEnum");
        headInfoList.add("import " + Constants.PACKAGE_EXCEPTION + ".BusinessException");
        headInfoList.add("import " + Constants.PACKAGE_VO + ".ResponseVO");
        headInfoList.add("import org.slf4j.Logger");
        headInfoList.add("import org.slf4j.LoggerFactory");
        headInfoList.add("import org.springframework.web.bind.annotation.ExceptionHandler");
        headInfoList.add("import org.springframework.web.bind.annotation.RestControllerAdvice");
        headInfoList.add("import org.springframework.web.servlet.NoHandlerFoundException");
        headInfoList.add("import javax.servlet.http.HttpServletRequest");
        headInfoList.add("import java.net.BindException");
        headInfoList.add("import java.util.DuplicateFormatFlagsException");
        build(headInfoList, "AGlobalExceptionHandlerController", Constants.PATH_CONTROLLER);
    }

    private static void build(List<String> headInfoList, String fileName, String outPutPath) {
        File folder = new File(outPutPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        File javaFile = new File(outPutPath, fileName + ".java");

        try {
            OutputStream outputStream = Files.newOutputStream(javaFile.toPath());
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            String templatePath = BuildBase.class.getClassLoader().getResource("template/" + fileName + ".txt").getPath();
            InputStream inputStream = new FileInputStream(templatePath);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            for (String head : headInfoList) {
                bufferedWriter.write(head + ";");
                bufferedWriter.newLine();
                bufferedWriter.newLine();
            }
            String lineInfo;
            while ((lineInfo = bufferedReader.readLine()) != null) {
                bufferedWriter.write(lineInfo);
                bufferedWriter.newLine();
            }
            bufferedWriter.flush();
        } catch (IOException e) {
            logger.error("生成基础类：{},失败：", fileName, e);
        }
    }
}

